#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# from machineLearning.settings import BASE_DIR
import json
import os

MOD_SET = 1
MOD_MAP = 2


def write_dist(dists, path):
    for d in dists:
        p = path + '/' + d  # 文件名
        content = dists[d]
        # 这里的buffer暂时不知道是什么意思
        with open(p, 'w', 1, 'utf-8') as f:
            for c in content:
                f.write(c + '\n')


def write_data(data, path, mod=MOD_SET):
    output = open(path, 'w', 1, 'utf-8')
    if mod == MOD_SET:
        for i in range(len(data)):
            output.write(data[i] + '\n')
            pass
        output.close()
        pass
    pass


def write_json(data, path):
    with open(path, 'w') as file:
        file.write(json.dumps(data))
        pass
    pass

#
# def mv_file_upload(request, key, path=None, file_name=None):
#     try:
#         if path is None:
#             path = os.path.join(BASE_DIR, 'static')
#             pass
#         else:
#             path = os.path.join(BASE_DIR, 'static', path)
#             pass
#         if request.method == 'POST':
#             # 判断路径是否存在
#             if not os.path.exists(path):
#                 os.makedirs(path)
#                 pass
#             f = request.FILES.get(key)
#             if file_name is None:
#                 file_name = f.name
#                 pass
#             file_path = os.path.join(path, file_name)
#             fobj = open(file_path, 'wb')
#             for chunk in f.chunks():
#                 fobj.write(chunk)
#                 pass
#             fobj.close()
#             results = {
#                 'code': 200,
#                 'msg': '保存成功',
#                 'data': file_path,  # 保存文件的路径
#             }
#             return results
#             pass
#         else:
#             results = {
#                 'code': 201,
#                 'msg': 'django提交文件需指定method为POST'
#             }
#             return results
#             pass
#     except IOError:
#         return {
#             'code': 500,
#             'msg': '程序内部错误',
#         }
#         pass
#     pass
